﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class SubPolicyKnow : System.Web.UI.Page
    {
        protected int subId = 0;
        protected int SubjectId = 0;
        public static Module_Page Pages = new Module_Page();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterUser"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["subId"] != null && Request.QueryString["subId"] != string.Empty)
            {
                subId = int.Parse(Request.QueryString["subId"]);
            }
            if (Request.QueryString["SubjectId"] != null && Request.QueryString["SubjectId"] != string.Empty)
            {
                SubjectId = int.Parse(Request.QueryString["SubjectId"]);
            }
            if (!IsPostBack)
            {
                try
                {
                    ShowSubjectInfo();
                    ShowBookInfo();
                    ShowKonwPoint();
                    LoadList();
                }
                catch
                {

                }
            }
        }
        void ShowSubjectInfo()
        {
            if (SubjectId > 0)
            {
                SubjectInfo subInfo = Subject.GetSubjectInfoById(SubjectId);
                if (subInfo != null && subInfo.Id > 0)
                {
                    this.lbSujbect.Text = subInfo.Name;
                }
            }
           
        }
        void ShowBookInfo()
        {
            if (SubjectId > 0)
            {
                IList<BookInfo> list = Book.GetBookInfosBySubject(SubjectId);
                this.chkBooks.DataTextField = "Name";
                this.chkBooks.DataValueField = "Id";
                this.chkBooks.DataSource = list;
                this.chkBooks.DataBind();
            }
            
        }
        void ShowKonwPoint()
        {
            drpKownPoint.Items.Clear();
            if (SubjectId>0)
            {
                //IList<KnowledgeInfo> list = Knowledge.GetKnowledgeInfosBySubjectId();

                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetRootKnowledgeInfo(0, SubjectId);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(info.Keyword + "  " + info.Name, info.Id.ToString());//+ "    " + info.Keyword
                    drpKownPoint.Items.Add(item);
                    //item = new ListItem(info.Keyword, info.Keyword);
                    LoadKnowledge(info.Id, SubjectId, "|——");
                }
                //this.drpKownPoint.DataTextField = "Name";
                //this.drpKownPoint.DataValueField = "Id";
                //this.drpKownPoint.DataSource = list;
                //this.drpKownPoint.DataBind();
            }
            drpKownPoint.Items.Insert(0, new ListItem("选择", ""));

        }

        protected void LoadKnowledge(int ParentID, int inputSubjectId, string line)
        {
            IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetKnowledgeInfos(ParentID);//通过parentId获取子集合
            foreach (KnowledgeInfo info in knowledgeInfos)
            {
                ListItem item = new ListItem(line + info.Keyword + "  " + info.Name, info.Id.ToString());//+ "    " + info.Keyword
                drpKownPoint.Items.Add(item);
                LoadKnowledge(info.Id, inputSubjectId, "|——" + line);
            }
        }

        protected void lbLeve_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    KnowledgeInfo dic = Knowledge.GetKnowledgeInfoById( int.Parse(lb.Text));
                    if (dic != null && dic.Id != 0)
                    {
                        lb.Text = dic.Keyword +"  "+ dic.Name;
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }

        protected void LoadList()
        {
            try
            {
                TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                if (sub != null && sub.Id !=0)
                {
                    XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                    if (xmlQuestion != null)
                    {
                        if (xmlQuestion.BookIds != null && xmlQuestion.BookIds.Count() > 0)
                        {
                            for (int i = 0; i < this.chkBooks.Items.Count; i++)
                            {
                                if (xmlQuestion.BookIds.Contains(this.chkBooks.Items[i].Value))
                                {
                                    this.chkBooks.Items[i].Selected = true;
                                }
                            }
                        
                        }

                        this.UserLists.DataSource = xmlQuestion.KnowPoints;
                        this.UserLists.DataBind();
                    }
                }
            }
            catch
            {
                
            }
        }

        protected void lbDelete_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            if (lb != null && subId!=0)
            {
                try
                {
                    string diffId = lb.CommandArgument;
                    TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                    if (sub != null && sub.Id != 0 && diffId != "")
                    {
                        XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                        if (xmlQuestion.KnowPoints!= null)
                        {
                            for (int i = 0; i < xmlQuestion.KnowPoints.Count(); i++)
                            {
                                XmlKnowPoint know = xmlQuestion.KnowPoints[i];
                                if (know.KnowPointId == diffId)
                                {
                                    xmlQuestion.KnowPoints.Remove(know);
                                    break;
                                }
                            }
                        }
                        sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                        TestPaperPolicySub.Edit(sub);

                        if (sub != null && sub.Id !=0)
                        {
                            if (xmlQuestion != null)
                            {
                                this.UserLists.DataSource = xmlQuestion.KnowPoints;
                                this.UserLists.DataBind();
                            }
                            string value = diffId;
                            this.ClientScript.RegisterStartupScript(this.GetType(), "key", " RemoveSelect('BookMaster','lbKownPoint','" + value + "');", true);
                        }
                        else
                        {
                            this.ClientScript.RegisterStartupScript(this.GetType(), "key", "OnSucceeded('failure')", true);

                        }

                    }
                }
                catch
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "key", "OnSucceeded('failure')", true);
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(subId);
                if (sub != null && sub.Id != 0 )
                {

                    XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                    if (SubjectId > 0)
                    {
                        xmlQuestion.SubjectId = SubjectId.ToString();
                    }
                    xmlQuestion.BookIds = new List<string>();
                    for (int i = 0; i < chkBooks.Items.Count;i++ )
                    {
                        if (chkBooks.Items[i].Selected)
                        {
                            xmlQuestion.BookIds.Add(chkBooks.Items[i].Value);
                        }
                    }
                    if (this.drpKownPoint.SelectedValue != "")
                    {
                        #region
                        if (xmlQuestion.KnowPoints == null)
                        {
                            xmlQuestion.KnowPoints = new List<XmlKnowPoint>();
                        }
                        XmlKnowPoint diff = new XmlKnowPoint
                        {
                            KnowPointId = this.drpKownPoint.Items[this.drpKownPoint.SelectedIndex].Value,
                            KnowPointTypePercent = double.Parse(this.drpPercent.Items[this.drpPercent.SelectedIndex].Value)
                        };
                        var query = from item in xmlQuestion.KnowPoints
                                    where item.KnowPointId == this.drpKownPoint.SelectedValue
                                    select item;
                        if (query.Count() == 0)
                        {
                            xmlQuestion.KnowPoints.Add(diff);
                            sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                            TestPaperPolicySub.Edit(sub);
                            if (sub != null && sub.Id != 0)
                            {
                                if (xmlQuestion != null)
                                {
                                    this.UserLists.DataSource = xmlQuestion.KnowPoints;
                                    this.UserLists.DataBind();
                                }
                                string text1=drpKownPoint.SelectedItem.Text;
                                text1 = text1.Replace("|——", "").Trim();
                                string text = text1 + "点" + string.Format("{0:0%}", this.drpPercent.Items[this.drpPercent.SelectedIndex].Text);
                                string value = this.drpKownPoint.SelectedValue;
                                ClientScript.RegisterStartupScript(this.GetType(), "key", " AddToSelect('BookMaster','lbKownPoint','" + value + "','" + text + "');", true);
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "key", " OnSucceeded('failure');", true);
                            }
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "key", " OnSucceeded('exist');", true);
                        }

                        #endregion
                    }
                    else
                    {
                        sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                        TestPaperPolicySub.Edit(sub);
                        if (sub != null && sub.Id != 0)
                        {
                            if (xmlQuestion != null)
                            {
                                this.UserLists.DataSource = xmlQuestion.KnowPoints;
                                this.UserLists.DataBind();
                            }

                            ClientScript.RegisterStartupScript(this.GetType(), "key", " OnSucceeded('sucess');", true);
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "key", " OnSucceeded('failure');", true);
                        }
                    }
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "key", " OnSucceeded('failure');", true);
            }
        }
    }
}

